package com.rljk.controller.app;

import com.google.gson.JsonObject;
import com.rljk.common.controller.BaseController;
import com.rljk.common.result.CodeMsg;
import com.rljk.common.result.Result;
import com.rljk.domain.common.PageData;
import com.rljk.redis.RedisService;
import com.rljk.service.TrustSqlManager;
import com.rljk.service.impl.AssetsService;
import com.rljk.service.impl.TrustSqlService;
import com.rljk.utils.*;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.o;

/**
 * Author:   YASUO
 * Date:     2018/7/21 16:36
 * Description
 */
@RestController
@RequestMapping("/assets")
@Slf4j
public class AssetsInController extends BaseController{

    @Resource(name = "assetsService")
    private AssetsService assetsService;

    @Resource(name = "trustSqlService")
    private TrustSqlService trustSqlService;

    @Autowired
    RedisService redisService;

    /**
     * TrustSql身份管理-获取用户信息
     * @Description
     * @Author zhaozhenhua
     * @date   2018年7月26日
     */
    @RequestMapping(value="/usercertfetch")
    //@Encrypt
    public Object userCertFetch() {
        log.info("TrustSql身份管理-获取用户信息");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            /*if (Tools.checkKey("NEWSDETAIL", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("getNewsDetail",pd)){*/
            Object o = trustSqlService.userCertFetch(pd);
            return Result.success(o);
               /* }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }*/
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }


    /**
     * app我的宝石获取排位
     * @Description
     * @Author zhaozhenhua
     * @date   2018年8月11日
     */
    @RequestMapping(value="/getgemrankredis")
    //@Encrypt
    public Object getGemRankRedis() {
        log.info("app我的宝石获取排位");
        PageData pd = new PageData();
        pd = this.getPageData();
        log.info("请求参数："+pd.toString());
        Map<String,Object> map = new HashMap<>();
        try {
            if (Tools.checkKey("GEMRANK", pd.getString("FKEY"))) {
                if (AppUtil.checkParam("useridParam",pd)){
                    //1.获取用户信息
                    String USER_ID = redisService.get(pd.getString("TOKEN"));
                    pd.put("USER_ID",USER_ID);
                    Set<String> gemRankCache = assetsService.getGemRankCache();
                    if(gemRankCache.size()==0||gemRankCache==null){
                        assetsService.setGemRankCache();
                        gemRankCache = assetsService.getGemRankCache();
                    }
                    log.info("gemRankCache:"+gemRankCache);

                    List<JSONObject> gemRank = new ArrayList<>();
                    for (String s : gemRankCache) {
                        JSONObject jsonobject = JSONObject.fromObject(s);
                        gemRank.add(jsonobject);
                    }

                    PageData myData = assetsService.getGemRank(pd).get(0);
                    Long zrevrank = redisService.zrevrank(Const.MYSTONE, JSONObject.fromObject(myData).toString());

                    log.info("zrevrank:"+zrevrank);
                    map.put("rank",zrevrank+1);
                    map.put("gemstone", gemRank);
                    return Result.success(map);
                }else {
                    return Result.error(CodeMsg.PARAM_LACK);
                }
            } else {
                return Result.error(CodeMsg.FKEY_FAIL);
            }
        } catch (Exception e) {
            log.error(e.toString(), e);
            return Result.error(CodeMsg.SERVER_ERROR);
        }
    }

}
